<template>
    <el-dialog
        :title="titleMap[state.mode]"
        v-model="state.visible"
        :width="500"
        destroy-on-close
        @closed="$emit('closed')"
    >
        <el-form
            :model="form"
            :rules="rules"
            :disabled="state.mode == 'show'"
            ref="dialogFormRef"
            label-width="100px"
        >
            <el-form-item label="上级部门" prop="pid">
                <el-cascader
                    v-model="form.pid"
                    :options="state.groups"
                    :props="groupsProps"
                    :show-all-levels="false"
                    clearable
                    style="width: 100%"
                ></el-cascader>
            </el-form-item>
            <el-form-item label="部门名称" prop="label">
                <el-input v-model="form.label" placeholder="请输入部门名称" clearable></el-input>
            </el-form-item>
            <el-form-item label="排序" prop="sort">
                <el-input-number
                    v-model="form.sort"
                    controls-position="right"
                    :min="1"
                    style="width: 100%"
                ></el-input-number>
            </el-form-item>
            <el-form-item label="是否有效" prop="status">
                <el-switch v-model="form.status" active-value="1" inactive-value="0"></el-switch>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" clearable type="textarea"></el-input>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button @click="state.visible = false">取 消</el-button>
            <el-button
                v-if="state.mode != 'show'"
                type="primary"
                :loading="state.isSaving"
                @click="submit()"
                >保 存</el-button
            >
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue'
import type { DeptModel } from '@/views/setting/dept/type.ts'
import type { FormRules } from 'element-plus'
import setting from '@/api/modules/setting.ts'
import feedback from '@/utils/feedback.ts'
import type { TitleMapModel } from '@/views/setting/user/type.ts'

const emit = defineEmits(['success', 'closed'])

const dialogFormRef = ref()

const form = reactive<DeptModel>({
    id: 0,
    pid: 0,
    label: '',
    sort: 1,
    createTime: '',
    deleteTime: '',
    updateTime: '',
    status: '1',
    remark: '',
    deleteFlag: 0
})

const rules = reactive<FormRules>({
    sort: [{ required: true, message: '请输入排序', trigger: 'change' }],
    label: [{ required: true, message: '请输入部门名称' }]
})

const groupsProps = reactive({
    value: 'id',
    label: 'label',
    emitPath: false,
    checkStrictly: true
})

const titleMap = reactive<TitleMapModel>({
    add: '新增用户',
    edit: '编辑用户',
    show: '查看'
})

const state = reactive({
    mode: 'add',
    visible: false,
    isSaving: false,
    // 所需数据选项
    groups: []
})

onMounted(() => {
    getGroup()
})

defineExpose({
    open,
    setData
})

// 显示
function open(mode = 'add') {
    state.mode = mode
    state.visible = true
}

// 加载树数据
async function getGroup() {
    state.groups = await setting.dept.list.get()
}

// 表单提交方法
async function submit() {
    await dialogFormRef.value!.validate()
    state.isSaving = true
    const res = await setting.dept.edit.post(form)
    state.isSaving = false
    if (res) {
        emit('success', form, state.mode)
        state.visible = false
        feedback.success('操作成功')
    }
}

// 表单注入数据
function setData(data: DeptModel) {
    form.id = data.id
    form.label = data.label
    form.status = data.status
    form.sort = data.sort
    form.pid = data.pid
    form.remark = data.remark
    form.deleteFlag = data.deleteFlag

    // 可以和上面一样单个注入，也可以像下面一样直接合并进去
    // Object.assign(this.form, data)
}
</script>

<style></style>
